package com.bookShop.dao.impl;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.bookShop.entity.BookOrder;
import com.bookShop.tool.DBConnection;





public class BookOrderDao {
	Connection conn =DBConnection.getConnection();
	PreparedStatement ps = null;
	ResultSet rs = null;
	public int insert(BookOrder bookorder){
		
		int n = 0;
		String sql="insert into bookorder (id,memberId,address,tel,price,bz,isDeliver,memberName,bookName) values(null,?,?,?,?,?,?,?,?)";
	
		try{
			ps=conn.prepareStatement(sql);
			ps.setInt(1, bookorder.getMemberId());
			ps.setString(2, bookorder.getAddress());
			ps.setString(3, bookorder.getTel());
			ps.setDouble(4, bookorder.getPrice());
			ps.setString(5, bookorder.getBz());
			ps.setInt(6,bookorder.getIsDeliver());
			ps.setString(7, bookorder.getMemberName());
			ps.setString(8, bookorder.getBookName());
			n = ps.executeUpdate();
		}catch (Exception e) {
			e.printStackTrace();
		}finally{
		}
		return n;
	}
	public List<BookOrder> selectAll() {
		// TODO Auto-generated method stub
		String sql = "select * from bookorder";
		List<BookOrder> bookorders = new ArrayList<BookOrder>();
		try{
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while(rs.next()){
				BookOrder bookOrder = new BookOrder();
				bookOrder.setId(rs.getInt("id"));
				bookOrder.setBookName(rs.getString("bookName"));
				bookOrder.setMemberName(rs.getString("memberName"));
				bookOrder.setMemberId(rs.getInt("memberId"));
				bookOrder.setPrice(rs.getDouble("price"));
				bookOrder.setIsDeliver(rs.getInt("isDeliver"));
				bookorders.add(bookOrder);
			}
		}catch (SQLException e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally{
			try{
				conn.close();
			}catch (SQLException e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		}
		return bookorders;
	}
	
	public int delete(int id){
		int isDelete = 0;
		String sql = "delete from bookorder where id = ?";
		conn = DBConnection.getConnection();
		try{
			ps = conn.prepareStatement(sql);
			ps.setInt(1, id);
			isDelete = ps.executeUpdate();
		}catch (SQLException e) {
		// TODO: handle exception
			e.printStackTrace();
		}finally{
			try{
				conn.close();
			}catch (SQLException e) {
			// TODO: handle exception
			e.printStackTrace();
			}
		}
		return isDelete;
	}
	
	
	public int updata(int id){
		int isupdata = 0;
		String sql = "update bookorder set isDeliver=? where id = ?";
		conn = DBConnection.getConnection();
		try{
			ps = conn.prepareStatement(sql);
			ps.setInt(1, 1);
			ps.setInt(2, id);
			
			isupdata = ps.executeUpdate();
		}catch (SQLException e) {
		// TODO: handle exception
			e.printStackTrace();
		}finally{
			try{
				conn.close();
			}catch (SQLException e) {
			// TODO: handle exception
			e.printStackTrace();
			}
		}
		return isupdata;
	}
	
	public List<BookOrder> selectMyOrder(int id){
		String sql = "select * from bookorder where memberId="+id;
		List<BookOrder> bookorders = new ArrayList<BookOrder>();
		try{
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while(rs.next()){
				BookOrder bookOrder = new BookOrder();
				bookOrder.setId(rs.getInt("id"));
				bookOrder.setBookName(rs.getString("bookName"));
				bookOrder.setMemberName(rs.getString("memberName"));
				bookOrder.setMemberId(rs.getInt("memberId"));
				bookOrder.setPrice(rs.getDouble("price"));
				bookOrder.setIsDeliver(rs.getInt("isDeliver"));
				bookorders.add(bookOrder);
			}
		}catch (SQLException e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally{
			try{
				conn.close();
			}catch (SQLException e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		}
		return bookorders;
	}
}